<!--管理员添加公司-->
<link rel="stylesheet" href="./css/gongsi.css">

<!--添加账号模态框-->
<div class="modal fade" id="myModal_add" tabindex="-1" role="dialog" aria-labelledby="myModal_add">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title">添加账号</h4>
			</div>
			<div class="modal-body">
				<div class="input-group margintop">
					<span class="input-group-addon">公司</span>
					<select id="gongsi2">

					</select>
				</div>
				<div class="input-group">
					<span class="input-group-addon">账号</span>
					<input id="gs_zhanghao" type="text" class="form-control" placeholder="请输入账号" autocomplete="off">
				</div>
				<div class="input-group">
					<span class="input-group-addon">密码</span>
					<input id="gs_zhanghao_passwd" type="password" class="form-control" placeholder="请输入密码" autocomplete="off">
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-primary" data-dismiss="modal" id="gs_ok_add">确定添加</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>
</div>

<div class="modal-dialog modal fade" id="submit1">
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal">&times;</button>
			<h4>提示</h4>
		</div>
		<div class="modal-body">
			<p>提交成功！</p>
		</div>
		<div class="modal-footer">
			<button type="button" class="btn btn-primary" class="close" data-dismiss="modal">确认</button>
			<button type="button" class="btn btn-default" class="close" data-dismiss="modal">取消</button>
		</div>
	</div>
</div>
<div class="modal-dialog modal fade" id="shangchuan">
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal">&times;</button>
			<h4>提示</h4>
		</div>
		<div class="modal-body">
			<p>上传成功！</p>
		</div>
		<div class="modal-footer">
			<button type="button" class="btn btn-primary" class="close" data-dismiss="modal">确认</button>
			<button type="button" class="btn btn-default" class="close" data-dismiss="modal">取消</button>
		</div>
	</div>
</div>
<div class="modal-dialog modal fade" id="shibai">
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal">&times;</button>
			<h4>提示</h4>
		</div>
		<div class="modal-body">
			<p>提交失败！</p>
		</div>
		<div class="modal-footer">
			<button type="button" class="btn btn-primary" class="close" data-dismiss="modal">确认</button>
			<button type="button" class="btn btn-default" class="close" data-dismiss="modal">取消</button>
		</div>
	</div>
</div>
<div class="modal-dialog modal fade" id="shangchuan1">
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal">&times;</button>
			<h4>提示</h4>
		</div>
		<div class="modal-body">
			<p>上传失败！</p>
		</div>
		<div class="modal-footer">
			<button type="button" class="btn btn-primary" class="close" data-dismiss="modal">确认</button>
			<button type="button" class="btn btn-default" class="close" data-dismiss="modal">取消</button>
		</div>
	</div>
</div>

<h3 class="text-center">添加公司</h3>

<div class="input-group  margintop">
	<span class="input-group-addon" id="gongsi">公司名字</span>
	<input type="text" id="gongsi1" class="form-control" placeholder="请输入公司名字" aria-describedby="sizing-addon1">
</div>
<div class="input-group  margintop">
	<span class="input-group-addon" id="address">公司地址</span>
	<input type="text" id="address1" class="form-control" placeholder="请输入公司地址" aria-describedby="sizing-addon1">
</div>
<div class="input-group margintop">
	<span class="input-group-addon" id="contact">联系人名称</span>
	<input type="text" id="contact1" class="form-control" placeholder="请输入联系人名称" aria-describedby="sizing-addon1">
</div>
<div class="input-group margintop">
	<span class="input-group-addon" id="phone">联系人电话</span>
	<input type="text" id="phone1" class="form-control" placeholder="请输入联系人电话" aria-describedby="sizing-addon1">
</div>
<a class="a_cAdmin">添加公司联系人账号></a>
<button type="button" class="btn btn-primary" id="submit2">提交信息</button>
<button id="daoru" type="button" class="btn btn-primary">批量导入公司</button>

<script>
	$('#submit1').on('hidden.bs.modal', function() {
		$('.center').html('');
		$('.center').load('./gongsi.html');
	})

	$('.a_cAdmin').click(function() {
		var myajax = $.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: 'GET',
			url: getFullURL("get-companys"),
			data: {},
			dataType: 'json',
			success: function(data) {
				if(data.status == 0) {
					console.log(data);
					$.each(data.list, function(i, n) {
						var str = `
							<option value="` + n.cid + `">` + n.cyName + `</option>
						`;

						$('#gongsi2').append(str);
					})
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})

		$.when(myajax).done(function() {
			$(myModal_add).modal({
				keyboard: true,
				show: true
			})
		});
	})

	//添加公司
	$('#submit2').click(function() {
		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: 'POST',
			url: getFullURL("admin/add-company"),
			data: {
				cyName: $('#gongsi1').val(),
				cyAddress: $('#address1').val(),
				cyContacts: $('#contact1').val(),
				cyPhone: $('#phone1').val()
			},
			dataType: 'json',
			success: function(data) {
				if(data.status == 0) {
					$('#submit1').modal({
						keyboard: true,
						show: true
					})
				} else if(data.status == -1) {
					$('#shibai').modal({
						keyboard: true,
						show: true
					})
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	})

	//添加公司联系人账号
	$('#gs_ok_add').click(function() {
		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: 'POST',
			url: getFullURL("admin/add-cAdmin"),
			data: {
				cano: $('#gs_zhanghao').val(),
				password: $('#gs_zhanghao_passwd').val(),
				cid: $('#gongsi2').val()
			},
			dataType: 'json',
			success: function(data) {
				if(data.status == 0) {
					$('#submit1').modal({
						keyboard: true,
						show: true
					})
				} else if(data.status == -1) {
					$('#shibai').modal({
						keyboard: true,
						show: true
					})
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	})
</script>